package com.xszx.serviceedu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xszx.servicebase.exceptionhandler.QinXueException;
import com.xszx.serviceedu.entity.Chapter;
import com.xszx.serviceedu.entity.Course;
import com.xszx.serviceedu.entity.Video;
import com.xszx.serviceedu.entity.vo.ChapterVo;
import com.xszx.serviceedu.entity.vo.VideoVo;
import com.xszx.serviceedu.mapper.ChapterMapper;
import com.xszx.serviceedu.service.ChapterService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xszx.serviceedu.service.CourseService;
import com.xszx.serviceedu.service.VideoService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author 白熊
 * @since 2024-07-17
 */
@Service
public class ChapterServiceImpl extends ServiceImpl<ChapterMapper, Chapter> implements ChapterService {

    @Autowired
    private VideoService videoService;
    @Override//根据id查询章节的嵌套信息
    public List<ChapterVo> nestedList(String courseId) {
        List<ChapterVo> result = new ArrayList<>();
        // 根据课程id查询所有的章节
        QueryWrapper<Chapter> wrapper1 = new QueryWrapper<>();
        wrapper1.eq("course_id",courseId);
        wrapper1.orderByAsc("sort", "id"); // 升序
        List<Chapter> chapterList = baseMapper.selectList(wrapper1);//查询

        // 2. 根据课程id查询所有的小节
        QueryWrapper<Video> wrapper2 = new QueryWrapper<>();
        wrapper2.eq("course_id",courseId);
        wrapper2.orderByAsc("sort","id");
        List<Video> videoList = videoService.list(wrapper2);

        // 3. 将章节和小节组装好
        for (Chapter chapter : chapterList) {
            ChapterVo chapterVo = new ChapterVo();
            BeanUtils.copyProperties(chapter,chapterVo);//赋值给列表信息
            result.add(chapterVo);
            List<VideoVo> children = new ArrayList<>();//子
            for (Video video : videoList) {
                //如果是节目录的上级章目录id和该循环的父循环章目录id相同的话
                if(video.getChapterId().equals(chapter.getId())){
                    VideoVo videoVo = new VideoVo();
                    BeanUtils.copyProperties(video,videoVo);
                    children.add(videoVo);//添加到子
                }
            }
            chapterVo.setChildren(children);
        }
        return result;
    }

    @Override//根据id删除章目录 假如下面有节目录,就不能删除
    public Boolean removeChapterById(String id) {
        // 1. 根据该章节id，查询它是否有小节
        QueryWrapper<Video> wrapper = new QueryWrapper<>();
        wrapper.eq("chapter_id",id);
        List<Video> videoList = videoService.list(wrapper);
        // 2. 如果没有小节，直接删除章节
        if(videoList ==null || videoList.size()==0) {
            int i = baseMapper.deleteById(id);
            return i>0;
        }else {
            // 3. 如果有小节，不能删除，给与提示！
            throw  new QinXueException(20001,"该章节下面有小结,请删除小结后再删除章节!");
        }
    }

    @Override//根据课程id删除章目录
    public void removeByCourseId(String courseId) {
        QueryWrapper<Chapter> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id",courseId);
        int count = baseMapper.selectCount(wrapper);
        if(count!=0){
            Integer i = baseMapper.delete(wrapper);
            if(i==0){
                throw new QinXueException(20001,"章目录删除失败");
            }
        }
    }
}
